package yanglib.application.metadata.message;

import yanglib.message.Message;
import yanglib.session.Request;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 9/6/14
 * Time: 8:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class PutMessage extends Request {
    private byte[] key;
    private byte[] value;
    public PutMessage(ByteBuffer buffer) {
        super(buffer);
    }

    public PutMessage(int sender, byte[] key, byte[] value) {
        super(MetadataMessageDeserializer.PUT_MESSAGE, sender);
        this.key = key;
        this.value = value;
    }

    public byte[] getKey(){
        return this.key;
    }

    public byte[] getValue(){
        return this.value;
    }

    @Override
    public void serialize(ByteBuffer buffer){

        super.serialize(buffer);
        buffer.putInt(key.length);
        buffer.put(key);
        buffer.putInt(value.length);
        buffer.put(value);
    }

    @Override
    public void deserialize(ByteBuffer buffer) throws IOException {
        super.deserialize(buffer);
        key = new byte[buffer.getInt()];
        buffer.get(key);
        value = new byte[buffer.getInt()];
        buffer.get(value);
    }

    @Override
    public String toString(){
        return "Put " + this.getRequestID() + " key="+ Arrays.toString(key)+" value= "+Arrays.toString(value);
    }
}
